package com.yinggu.utils;


import org.apache.commons.dbutils.ResultSetHandler;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.*;

public class DBUtil {

    private static String driver;
    private static String url;
    private static String user;
    private static String pwd;

    static {
        // 读取属性文件
        Properties pt = new Properties();
        InputStream resourceAsStream = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            pt.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取属性文件信息
        driver = pt.getProperty("driver");
        url = pt.getProperty("url");
        user = pt.getProperty("user");
        pwd = pt.getProperty("pwd");
        // 注册驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    // 获取一个连接
    public static Connection getConn() {
        try {
            Connection connection = DriverManager.getConnection(url, user, pwd);
            return connection;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    // 增删改
    public static int update(String sql, Object...params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            for (int i=1;i<=params.length;i++) {
                ps.setObject(i, params[i-1]);
            }
            return ps.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            close(ps, conn);
        }
        return 0;
    }
    // 查询
    public static Object query(ResultSetHandler rsh, String sql, Object...params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            for (int i=1;i<=params.length;i++) {
                ps.setObject(i, params[i-1]);
            }
            rs = ps.executeQuery();
            return rsh.handle(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(rs, ps, conn);
        }
        return null;
    }
    // 返回第一行第一列
    public static Object first(String sql, Object...params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            for (int i=1;i<=params.length;i++) {
                ps.setObject(i, params[i-1]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                return rs.getObject("first");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(rs, ps, conn);
        }
        return null;
    }
    // 关闭连接
    public static void close(PreparedStatement ps, Connection conn) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    // 关闭连接
    public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

